"use strict";
var prefix = "/report/naturalPerson";
var ssId = $("#ssId").val()
$(function(){
	$("body").on("click",".basic-type-img",function(){
		$(this).parent().siblings(".detail-type").slideToggle();
		if($(this).hasClass("basic-type-img-bottom")){
			$(this).removeClass("basic-type-img-bottom");
			$(this).addClass("basic-type-img-top");
		}else if($(this).hasClass("basic-type-img-top")){
			$(this).removeClass("basic-type-img-top");
			$(this).addClass("basic-type-img-bottom");
		}
	})

	$("body").on("click",".detail-type-img",function(){
		$(this).parent().siblings("table").fadeToggle();
		if($(this).hasClass("detail-type-img-sub")){
			$(this).removeClass("detail-type-img-sub");
			$(this).addClass("detail-type-img-add");
		}else if($(this).hasClass("detail-type-img-add")){
			$(this).removeClass("detail-type-img-add");
			$(this).addClass("detail-type-img-sub");
		}
	})
	init(ssId)
})

/**
 * 查询当前信用记录报告
 * @returns
 */
function init(ssId){
	$.ajax({
		data : {
			ssId : ssId
		}, 
		url : prefix + '/viewSnapShot' ,
		type : "POST",
		success : function(r) {
			if (r.code != 0) {
				layer.msg(r.msg);
			}else{
				var list = r.list[0];
				$(".main").html(list.ss_content);
				$('#QRCode').empty();
				$('#QRCode').qrcode({
			          render:"canvas",
			          height:120,
			          width:120,
			          correctLevel:0,
			          text:list.ss_qrcode
			      }); 
			}
		}
	})
}

/**
 * 查询明细
 * @param naturalPersonId	自然人ID
 * @param busiId	指标ID
 * @param _this	当前元素节点
 * @returns
 */
function queryDetail(naturalPersonId,busiId,_this){
	if($(_this).parents("tr").next().find(".detail-message").length != 0){
		$(_this).parents("tr").next().fadeToggle();
	}else{
		$.ajax({
			data : {
				naturalPersonId : naturalPersonId,
				busiId : busiId
			}, 
			url : prefix + '/linkReport' ,
			success : function(r) {
				if(r.th.length <=0){
					window.open("/report/legalPerson/viewDetail/" + r.tr[0].legal_person_id)
				}else{
					var Str = `<tr>
									<td colspan="2">
										<div>
											<table class="detail-message">
												<tbody>
													<tr>`
							$.each(r.th,function(key1,value1){
								Str += `				<th>${value1}</th>`
							})
								Str +=				`</tr>`
							$.each(r.tr,function(key2,value2){
								Str +=				`<tr>`
								$.each(value2,function(key3,value3){
										Str +=			`<td>${value3}</td>`
								})
								Str +=				`</tr>`
							})
							Str +=				`</tbody>
											</table>
										</div>
									</td>
								</tr>`;
					$(_this).parents("tr").after(Str)
				}
			}
		})
	}
}

/**
 * 查询法人信用报告
 * @param naturalPersonId	自然人ID
 * @param busiId	指标ID
 * @param _this	当前元素节点
 * @returns
 */
function queryLegal(legalPersonId,busiId,_this){
	if($(_this).parents("tr").next().find(".detail-message").length != 0){
		$(_this).parents("tr").next().fadeToggle();
	}else{
		$.ajax({
			data : {
				legalPersonId : legalPersonId,
				busiId : busiId
			}, 
			async: false,
			url : '/report/legalPerson/linkReport' ,
			success : function(r) {
				if(r.th.length <=0){
					window.open("/report/legalPerson/viewDetail/" + r.tr[0].legal_person_id)
				}else{
					var Str = `<tr>
									<td colspan="2">
										<div>
											<table class="detail-message">
												<tbody>
													<tr>`
							$.each(r.th,function(key1,value1){
								Str += `				<th>${value1}</th>`
							})
								Str +=				`</tr>`
							$.each(r.tr,function(key2,value2){
								Str +=				`<tr class="cursor" onclick="javaScript:window.open('/report/legalPerson/viewDetail/${value2.legal_person_id}')">`
								$.each(value2,function(key3,value3){
									if(key3 != "legal_person_id"){
										Str +=			`<td>${value3}</td>`
									}
								})
								Str +=				`</tr>`
							})
							Str +=				`</tbody>
											</table>
										</div>
									</td>
								</tr>`;
					$(_this).parents("tr").after(Str)
				}
			}
		})
	}
}

/**
 * 查询自然人信用报告
 * @param naturalPersonId	自然人ID
 * @param busiId	指标ID
 * @param _this	当前元素节点
 * @returns
 */
function queryNatural(naturalPersonId,busiId,_this){
	if($(_this).parents("tr").next().find(".detail-message").length != 0){
		$(_this).parents("tr").next().fadeToggle();
	}else{
		$.ajax({
			data : {
				naturalPersonId : naturalPersonId,
				busiId : busiId
			}, 
			url : '/report/naturalPerson/linkReport' ,
			success : function(r) {
				if(r.th.length <=0){
					window.open("/report/naturalPerson/viewDetail/" + r.tr[0].natural_person_id)
				}else{
					var Str = `<tr>
									<td colspan="2">
										<div>
											<table class="detail-message">
												<tbody>
													<tr>`
							$.each(r.th,function(key1,value1){
								Str += `				<th>${value1}</th>`
							})
								Str +=				`</tr>`
							$.each(r.tr,function(key2,value2){
								Str +=				`<tr class="cursor" onclick="javaScript:window.open('/report/legalPerson/viewDetail/${value2.natural_person_id}')">`
								$.each(value2,function(key3,value3){
									if(key3 != "natural_person_id"){
										Str +=			`<td>${value3}</td>`
									}
								})
								Str +=				`</tr>`
							})
							Str +=				`</tbody>
											</table>
										</div>
									</td>
								</tr>`;
					$(_this).parents("tr").after(Str)
				}
			}
		})
	}
}


function fun(){
	for(var i = 0;i<$("a").length;i++){
		if($($("a")[i]).attr("onclick") != undefined){
			$($("a")[i]).click();
		}
	}
	$("tr:hidden").fadeIn();
	$("table:hidden").fadeIn();
	$("div.detail-type:hidden").fadeIn();
}

function printReport(){
	window.open(prefix+"/printReport/"+naturalPersonId);
}
